"""VisitorSheet model for tracking property showings."""

from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy import ForeignKey, Integer, String, Text, Enum as SQLEnum, Date, Time
from sqlalchemy.orm import Mapped, mapped_column, relationship

from ..db.base_class import Base
from ..models.base import TimestampMixin
from ..models.enums import IntentionLevel

if TYPE_CHECKING:
    from ..models.lead import Lead
    from ..models.property import Property
    from ..models.user import User
    from ..models.follow_up_reminder import FollowUpReminder


class VisitorSheet(Base, TimestampMixin):
    """Visitor sheet entity for property showing records."""

    __tablename__ = "visitor_sheets"

    id: Mapped[int] = mapped_column(primary_key=True, index=True)

    # Foreign Keys
    lead_id: Mapped[int] = mapped_column(
        ForeignKey("leads.id"), nullable=False, index=True, comment="客户ID"
    )
    property_id: Mapped[int] = mapped_column(
        ForeignKey("properties.id"), nullable=False, index=True, comment="房源ID"
    )
    agent_id: Mapped[int] = mapped_column(
        ForeignKey("users.id"), nullable=False, index=True, comment="经纪人ID"
    )

    # Visit Details
    visit_date: Mapped[Date] = mapped_column(Date, nullable=False, index=True, comment="带看日期")
    visit_time: Mapped[Time | None] = mapped_column(Time, nullable=True, comment="带看时间")
    duration_minutes: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="停留时长(分钟)"
    )

    # Customer Feedback
    satisfaction: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="满意度(1-5星)"
    )
    pros_tags: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment="优点标签(逗号分隔)"
    )
    cons_tags: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment="缺点标签(逗号分隔)"
    )

    # Intention
    intention: Mapped[IntentionLevel] = mapped_column(
        SQLEnum(IntentionLevel), nullable=False, default=IntentionLevel.MEDIUM, comment="意向度"
    )

    # Notes
    notes: Mapped[str | None] = mapped_column(Text, nullable=True, comment="备注")

    # Relationships
    lead: Mapped[Lead] = relationship("Lead", back_populates="visitor_sheets")
    property: Mapped[Property] = relationship("Property", back_populates="visitor_sheets")
    agent: Mapped[User] = relationship(
        "User", back_populates="visitor_sheets", foreign_keys=[agent_id]
    )
    reminders: Mapped[list[FollowUpReminder]] = relationship(
        "FollowUpReminder", back_populates="visitor_sheet"
    )
